package com.winhxd.b2c.admin.module.order.controller;

import com.winhxd.b2c.admin.common.security.annotation.CheckPermission;
import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.context.AdminUser;
import com.winhxd.b2c.common.context.UserContext;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.*;
import com.winhxd.b2c.common.domain.order.vo.OrderAfterSaleDetailVO;
import com.winhxd.b2c.common.domain.order.vo.OrderAfterSaleVO;
import com.winhxd.b2c.common.domain.system.security.enums.PermissionEnum;
import com.winhxd.b2c.common.feign.order.OrderAfterSaleClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Api(value = "后台售后申请列表", tags = "后台售后申请列表")
@RequestMapping("/order")
//@CheckPermission(PermissionEnum.ORDER_MANAGEMENT_AFTERSALER_LIST)
public class OrderAfterSaleListController {

    private static final Logger logger = LoggerFactory.getLogger(OrderAfterSaleListController.class);

    @Resource
    private OrderAfterSaleClient orderAfterSaleClient;


    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_AFTERSALER_LIST)
    @ApiOperation(value = "售后申请列表", notes = "售后申请列表")
    @PostMapping(value = "/orderAfterSaleList")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<PagedList<OrderAfterSaleDetailVO>> orderAfterSaleList(@RequestBody OrderAfterSaleListCondition condition) {
        ResponseResult<PagedList<OrderAfterSaleDetailVO>> responseResult = orderAfterSaleClient.orderAfterSaleList(condition);
        return responseResult;
    }


    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_AFTERSALER_OPT)
    @ApiOperation(value = "售后申请页面按钮操作", notes = "售后申请页面按钮操作")
    @PostMapping(value = "/checkOrderAfterSale")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<OrderAfterSaleVO> checkOrderAfterSale(@RequestBody OrderAfterSaleCondition condition) {
        ResponseResult<OrderAfterSaleVO> responseResult = orderAfterSaleClient.checkOrderAfterSale(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_AFTERSALER_OPT)
    @ApiOperation(value = "售后审核页面按钮操作", notes = "售后审核页面按钮操作")
    @PostMapping(value = "/checkAfterSale")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<Integer> checkAfterSale(@RequestBody OrderAfterSaleCheckCondition condition) {
	    AdminUser adminUser = UserContext.getCurrentAdminUser();
	    condition.setResultUserName(adminUser.getUsername());
	    condition.setResultUser(String.valueOf(adminUser.getId()));
    	ResponseResult<Integer>  responseResult = orderAfterSaleClient.checkAfterSale(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_AFTERSALER_OPT)
    @ApiOperation(value = "售后立即退款页面按钮操作", notes = "售后立即退款页面按钮操作")
    @PostMapping(value = "/saveRefundInfo")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<Integer> saveRefundInfo(@RequestBody OrderAfterSaleRefundCondition condition) {
	    AdminUser adminUser = UserContext.getCurrentAdminUser();
	    logger.info("用户名"+adminUser.getUsername()+" "+"ID："+ adminUser.getCustomerId());
	    condition.setCreatedByName(adminUser.getUsername());
	    condition.setCreatedBy(String.valueOf(adminUser.getId()));
    	ResponseResult<Integer>  responseResult = orderAfterSaleClient.saveRefundInfo(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_AFTERSALER_OPT)
    @ApiOperation(value = "售后已发货换货页面按钮操作", notes = "售后已发货换货页面按钮操作")
    @PostMapping(value = "/saveExpressInfo4DeliveredGoods")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<Integer> saveExpressInfo4DeliveredGoods(@RequestBody OrderExpressCondition condition) {
	    AdminUser adminUser = UserContext.getCurrentAdminUser();
	    condition.setSendUser(String.valueOf(adminUser.getId()));
    	ResponseResult<Integer>  responseResult = orderAfterSaleClient.saveExpressInfo4DeliveredGoods(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_AFTERSALER_OPT)
    @ApiOperation(value = "售后已完成换货页面按钮操作", notes = "售后已完成换货页面按钮操作")
    @PostMapping(value = "/saveExpressInfo4Finished")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<Integer> saveExpressInfo4Finished(@RequestBody OrderExpressCondition condition) {
	    AdminUser adminUser = UserContext.getCurrentAdminUser();
	    condition.setSendUser(String.valueOf(adminUser.getId()));
        ResponseResult<Integer>  responseResult = orderAfterSaleClient.saveExpressInfo4Finished(condition);
        return responseResult;
    }

	@CheckPermission(PermissionEnum.ORDER_MANAGEMENT_AFTERSALER_OPT)
	@ApiOperation(value = "手动关闭售后按钮操作", notes = "手动关闭售后按钮操作")
	@PostMapping(value = "/closeAfterSale")
	@ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
	public ResponseResult<Void> closeAfterSale(@RequestBody OrderCloseAfterSaleCondition condition) {
		AdminUser adminUser = UserContext.getCurrentAdminUser();
		condition.setCustomerId(adminUser.getId());
		ResponseResult<Void> result = orderAfterSaleClient.closeAfterSale(condition);
		return result;
	}
}
